import { Controller, Get, Post, Query, Header, UseInterceptors, UploadedFile } from "@nestjs/common";
import { UeditorService } from "./ueditor.service";
import IResponse from "@/services/response.service";
import { FileInterceptor } from "@nestjs/platform-express";
import { Express } from "express";

@Controller("ueditor")
export class UeditorController {
  constructor(
    private readonly ueditorService: UeditorService
  ) {}

  @Get("handler")
  @Header('content-type', 'text/html; charset=utf-8')
  handlerGet(@Query() query: {action: string}) {
    if (query.action === "config") {
      return this.ueditorService.handlerConfig();
    }
    return IResponse.fail("非法请求");
  }

  @Post("handler")
  @Header('content-type', 'text/html; charset=utf-8')
  @UseInterceptors(FileInterceptor("upfile"))
  handlerPost(@Query() query: {action: string}, @UploadedFile() file: Express.Multer.File) {
    return this.ueditorService.handlerUpload(file);
  }
  
}